package com.edfu.core.commons.persistence.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.edfu.core.commons.persistence.api.INotificationPersistence;
import com.edfu.core.commons.persistence.entity.CategoryEntity;
import com.edfu.core.commons.persistence.entity.DeletedNotificationsEntity;
import com.edfu.core.commons.persistence.entity.InterestsNotificationEntity;
import com.edfu.core.identitymgt.persistence.entity.UserEntity;

@Repository
public class NotificationPersistenceImpl implements INotificationPersistence {
	
private static Logger logger = LoggerFactory.getLogger(NotificationPersistenceImpl.class);
	
	private EntityManager entityManager;
	
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	@Override
	public void addInterestsNotification(InterestsNotificationEntity interestsNotificationEntity) {
		entityManager.persist(interestsNotificationEntity);
	}
	
	@Override
	public InterestsNotificationEntity getInterestsNotification(long interestsNotificationId) {
		logger.debug("Fetching InterestsEntity for " + interestsNotificationId);
		return entityManager.find(InterestsNotificationEntity.class, interestsNotificationId);
	}

	@Override
	public void addDeletedInterestsNotification(DeletedNotificationsEntity deletedNotificationsEntity) {
		entityManager.persist(deletedNotificationsEntity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<InterestsNotificationEntity> getInterestsNotifications(List<CategoryEntity> categoryEntities) {
		Query query = entityManager.createQuery("select i from InterestsNotificationEntity i where i.category in (:categoryEntities)");
		query.setParameter("categoryEntities", categoryEntities);
		
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DeletedNotificationsEntity> getDeletedNotifications(UserEntity userEntity) {
		Query query = entityManager.createQuery("select i from DeletedNotificationsEntity i where i.user = :userEntity");
		query.setParameter("userEntity", userEntity);
		
		return query.getResultList();
	}

	@Override
	public void deleteInterestsNotification(long objectId, String objectType) {
		Query query = entityManager.createQuery("delete from InterestsNotificationEntity i where i.objectId = :objectId and i.objectType = :objectType");
		query.setParameter("objectId", objectId);
		query.setParameter("objectType", objectType);
		
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<InterestsNotificationEntity> getRecentContent(int size) {
		Query query = entityManager.createQuery("select i from InterestsNotificationEntity i order by i.createdOn desc");
		query.setMaxResults(size);
		
		return query.getResultList();
	}

}
